define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditiondroplets/index' + location.search,
                    add_url: 'feditiondroplets/add',
                    edit_url: 'feditiondroplets/edit',
                    del_url: 'feditiondroplets/del',
                    multi_url: 'feditiondroplets/multi',
                    import_url: 'feditiondroplets/import',
                    table: 'fedition_droplets',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'tags', title: __('Tags'), operate: 'LIKE'},
                        {field: 'ip_address', title: __('Ip_address')},
                        {field: 'droplets_status', title: __('状态'), searchList: {"active":__('Normal')}, formatter: Table.api.formatter.status,custom: {'active':'success'}},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('重启'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('一键重启'),
                                    title: __('一键重启'),
                                    classname: 'btn btn-sm btn-warning btn-magic btn-ajax',
                                    icon: 'fa fa-leaf',
                                    confirm: '等待时间约一分钟，确认开始重启吗？',
                                    url: 'feditiondroplets/edit_status',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        //table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            $(document).on('click', '.btn-droplets', function () {
                $.ajax({
                    dataType: 'json',
                    type: 'POST',
                    url: "feditiondroplets/droplets_iscustom",
                    data: {},
                    success: function (json) {
                        console.log('json',json);
                        Layer.closeAll();
                        Layer.alert('更新完成');
                        //刷新父级页面
                        window.parent.location.reload();
                        //刷新当前页面
                        table.bootstrapTable('refresh', {});
                        return true;
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        Layer.alert('更新失败');
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});